#!/usr/bin/env python

"""
Search through directories and find ENVISAT product files. Take them
and turn them into RDF data storing in our model. If a directory is
given on the command line, search it. Otherwise search through the
directories in the config file.
"""
__version__ = "$Id: $"
__usage__ = """
%prog [options]
%prog [options] directory"""
__epilog__ = """
The config file may have a section like the following
to specify which directories will be searched by default.
See the examples/envisat.cfg in the source directory.
"""

from sys import exit
from os import path, stat, walk
from duetopia.envisat import EnvisatProduct, EnvisatOptions, EnvisatConfig, EnvisatModel
import urllib2

if __name__ == '__main__':
	op = EnvisatOptions(version=__version__, description=__doc__,
			usage=__usage__, epilog = __epilog__)
	op.add_option("-f", "--flush",
			help="Flush any triples referring to the entry (destructive) [default False]",
			action="store_true",
			default=False,
	)
	op.add_option("-r", "--refresh",
			help="Update RDF if entry already exists [default False]",
			action="store_true",
			default=False,
	)
	op.add_option("-u", "--upload",
			help="Upload serialized RDF/XML to URL",
			action="store",
			default=False
	)

	(options, args) = op.parse_args()
	config = EnvisatConfig(options)

	if len(args) > 1:
		op.print_usage()
		exit()
	elif len(args) == 1:
		directory = args[0]
		stat(directory)
		directories = [directory]
	else:
		if not config.has_section("directories"):
			op.print_usage()
			exit()
		directories = map(lambda x: x[1], config.items("directories"))

	model = EnvisatModel(config)

	for directory in directories:
		for dir, sub, files in walk(directory):
			for filename in files:
				p = EnvisatProduct(config, path.join(dir, filename))
				if options.flush:
					p.flush(model)
				if options.refresh or not p.exists(model):
					if options.upload or config.has_option("envisat", "upload"):
						if options.upload:
							uri = options.upload
						else:
							uri = config.get("envisat", "upload")
						data = p.xml.serialize("utf-8", 1)
						request = urllib2.Request(uri, data)
						fp = urllib2.urlopen(request)
						fp.read()
						fp.close()
						## only save locally at the end so that we can
						## re-try if the server is down or broken
						p.parse_into_model(model)
					else:
						p.parse_into_model(model)
